package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.EventDAO;
import com.rainng.coursesystem.dao.MemberEventDAO;
import com.rainng.coursesystem.dao.LeaderDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.bo.EventItemBO;
import com.rainng.coursesystem.model.entity.EventEntity;
import com.rainng.coursesystem.model.entity.LeaderEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class EventManager extends BaseManager {
    private final LeaderDAO leaderDAO;
    private final EventDAO eventDAO;
    private final MemberEventDAO memberEventDAO;

    public EventManager(LeaderDAO leaderDAO, EventDAO eventDAO, MemberEventDAO memberEventDAO) {
        this.leaderDAO = leaderDAO;
        this.eventDAO = eventDAO;
        this.memberEventDAO = memberEventDAO;
    }

    public Integer getPageCount(String areaName, String leaderName, String name) {
        int count = eventDAO.count(areaName, leaderName, name);
        return calcPageCount(count, EventDAO.PAGE_SIZE);
    }

    public List<EventItemBO> getPage(Integer index, String areaName, String leaderName, String name) {
        return eventDAO.getPage(index, areaName, leaderName, name);
    }

    public EventEntity get(Integer id) {
        return eventDAO.get(id);
    }

    public int create(EventEntity entity) {
        return eventDAO.insert(entity);
    }

    public int update(EventEntity entity) {
        return eventDAO.update(entity);
    }

    public int delete(Integer id) {
        return eventDAO.delete(id);
    }

    public LeaderEntity getLeaderById(Integer leaderId) {
        return leaderDAO.get(leaderId);
    }

    public boolean hasMemberEvent(Integer eventId) {
        return memberEventDAO.countByEventId(eventId) > 0;
    }

    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<EventEntity> entityList = eventDAO.listName();
        for (EventEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName()));
        }

        return voList;
    }
}
